{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/hz/anaconda3/lib/python3.7/site-packages/sklearn/externals/joblib/__init__.py:15: DeprecationWarning: sklearn.externals.joblib is deprecated in 0.21 and will be removed in 0.23. Please import this functionality directly from joblib, which can be installed with: pip install joblib. If this warning is raised when loading pickled models, you may need to re-serialize those models with scikit-learn 0.21+.\n",
      "  warnings.warn(msg, category=DeprecationWarning)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.0\n",
      "0.9736842105263158\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.ensemble import GradientBoostingClassifier\n",
    "from sklearn.externals import joblib\n",
    "from sklearn.datasets import load_iris\n",
    "\n",
    "X,y = load_iris(return_X_y=True) # iris 花\n",
    "\n",
    "x_train, x_test, y_train, y_test = train_test_split(X, y)\n",
    "\n",
    "# 模型训练，使用GBDT算法\n",
    "gbr = GradientBoostingClassifier(n_estimators=3000, \n",
    "                                 max_depth=2, \n",
    "                                 min_samples_split=2, \n",
    "                                 learning_rate=0.1)\n",
    "gbr.fit(x_train, y_train.ravel())\n",
    "joblib.dump(gbr, 'train_model_iris.model')   # 保存模型\n",
    "\n",
    "y_gbr = gbr.predict(x_train)\n",
    "y_gbr1 = gbr.predict(x_test)\n",
    "acc_train = gbr.score(x_train, y_train)\n",
    "acc_test = gbr.score(x_test, y_test)\n",
    "print(acc_train)\n",
    "print(acc_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.externals import joblib\n",
    "\n",
    "\n",
    "# 加载模型并预测\n",
    "gbr = joblib.load('train_model_iris.model')    # 加载模型\n",
    "\n",
    "y_pre = gbr.predict(x_test)\n",
    "acc_test = gbr.score(x_test, y_test)\n",
    "print(acc_test)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
